package com.yugao.fintech.antelope.shop.process;

import com.yugao.fintech.antelope.base.model.module.auth.LoginUser;
import com.yugao.fintech.antelope.base.model.module.auth.enums.LoginWayEnum;
import com.yugao.fintech.antelope.shop.model.entity.UmsMember;
import com.yugao.fintech.antelope.shop.service.UmsMemberService;
import com.yugao.fintech.antelope.system.model.rqrs.log.LoginUserGetReq;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class FumUserProcess {
    @Autowired
    private UmsMemberService umsMemberService;

    public LoginUser getLoginUser(LoginUserGetReq req) {
        UmsMember umsMember;
        // 如果是第三方授权登录直接绑定, 要则先根据用户id查找用户, 如果找不到, 就插入数据
        if (!LoginWayEnum.LOCAL.getCode().equals(req.getLoginWay())) {
            if (Objects.isNull(req.getUserId())) {
                throw new BizException("当前为第三方授权登录, userId需要预先生成, 请检查代码");
            }
            umsMember = umsMemberService.getById(req.getUserId());
            if (Objects.isNull(umsMember)) {
                umsMember = umsMemberService.autoRegister(req);
            }
        } else {
            umsMember = umsMemberService.getById(req.getUserId());
            if (Objects.isNull(umsMember)) {
                throw new BizException("账号不存在");
            }
        }

        return new LoginUser().setUserName(umsMember.getUserName()).setStatus(umsMember.getStatus())
                .setUserId(umsMember.getUserId()).setUserName(umsMember.getUserName());
    }
}
